/**
* Copyright 2012 nabla
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package com.nabla.wapp.client.ui.form;

import java.util.Iterator;
import java.util.LinkedHashMap;

import com.google.gwt.user.client.ui.Widget;
import com.nabla.wapp.client.general.Assert;
import com.nabla.wapp.client.ui.IHasWidgets;
import com.nabla.wapp.client.ui.TitleDecoder;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;


public class RadioGroup extends FormItem<RadioGroupItem> implements IHasWidgets {

	private LinkedHashMap<String, String>	values = new LinkedHashMap<String, String>();

	public RadioGroup() {
		super(new RadioGroupItem());
		impl.setShowTitle(false);
		impl.setColSpan(2);
		impl.setWrapTitle(false);
		impl.setWrap(false);
	}

	public void setVertical(final Boolean vertical) {
		impl.setVertical(vertical);
	}

	public void setDefaultValue(final String value) {
		impl.setDefaultValue(value);
	}

	@Override
	public void setTitle(final String title) {
		new TitleDecoder(title).apply(impl);
		impl.setShowTitle(true);
		impl.setColSpan(1);
	}

	@Override
	public void add(Widget w) {
		w = Helper.onCreate(w);
		Assert.state(w instanceof RadioItem);
		final RadioItem item = (RadioItem)w;
		values.put(item.getKey(), item.getText());
	}

	@Override
	public RadioGroupItem getImpl() {
		if (values != null) {
			if (!values.isEmpty())
				impl.setValueMap(values);
			values = null;
		}
		return super.getImpl();
	}

	@Override
	public void clear() {}

	@Override
	public Iterator<Widget> iterator() {
		return null;
	}

	@Override
	public boolean remove(@SuppressWarnings("unused") Widget w) {
		return false;
	}
}
